package com.hlm.demo.service.roleinfo;

import com.hlm.demo.entity.RoleInfo;
import com.hlm.demo.entity.RoleModule;
import com.hlm.demo.entity.ex.RoleWithModule;
import com.hlm.demo.mapper.RoleInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleInfoService implements IRoleInfoService {
    @Autowired
    private static RoleInfoMapper roleInfoMapper;

    public RoleInfoService(RoleInfoMapper roleInfoMapper){
        RoleInfoService.roleInfoMapper = roleInfoMapper;
    }

    /**
     *
     * @return  遍历出所有的学生信息
     */
    @Override
    public List<RoleInfo> findAllRole() {
        return roleInfoMapper.findAllRole();
    }

    /**
     *
     * @return  查询出角色的数量
     */
    @Override
    public int roleQuantity() {
        return roleInfoMapper.roleQuantity();
    }

    /**
     * 根据角色的Id查询角色的信息
     * @param roleId
     * @return
     */
    @Override
    public RoleInfo findRoleById(Integer roleId) {
        return roleInfoMapper.findRoleById(roleId);
    }

    /**
     *
     * @param page
     * @param count
     * @return  查询出角色和其对应能操作的模块的信息
     */
    @Override
    public List<RoleWithModule> findRoleWithModule(int page,int count) {
        Map<String,Integer> map = new HashMap<>();
        try {
            page = (page - 1) * count;
        }catch (Exception e){
            page = 1;
            count = 5;
        }finally {

        map.put("page",page);
        map.put("count",count);

        return roleInfoMapper.findRoleWithModule(map);
        }

    }

    /**
     *
     * @param roleName
     * @param allModule
     * @return  添加一个角色和它所能对应的模块的方法
     */
    @Override
    public int addOneRole(String roleName,String allModule) {

        RoleInfo role = new RoleInfo();
        role.setRoleName(roleName);
        System.out.print("service层:" + role + "dff" + allModule);
        int i = roleInfoMapper.addOneRole(role);
        Integer roleId = role.getRoleId();

        if (allModule != null) {
           addModule(roleId,allModule);
        }

        return i;
    }

    /**
     *  删除一个角色,和这个角色所对应的能进行操作的模块
     * @param roleId
     * @return
     */

    @Override
    public int delRoleWithModule(Integer roleId) {


        int i = roleInfoMapper.delRole(roleId);
        roleInfoMapper.delRoleModule(roleId);

        return i;
    }

    /**
     *  修改一个角色的信息
     * @param roleWithModule
     * @return
     */
    @Override
    public int updRoleWithModule(RoleWithModule roleWithModule) {
        Integer roleId = roleWithModule.getRoleId();
        RoleInfo s_roleWithModule = roleInfoMapper.findRoleById(roleId);

        String roleName = roleWithModule.getRoleName();
        String allModule = roleWithModule.getAllModule();

        roleInfoMapper.delRoleModule(roleId);

        if (!s_roleWithModule.getRoleName().equals(roleName)){
            Map<String,Object> map = new HashMap<>();
            map.put("roleId",roleId);
            map.put("roleName",roleName);
         roleInfoMapper.updRole(map);
        }

        if (allModule != null){
            addModule(roleId,allModule);
          }

        return 0;
    }

    public static void addModule(Integer roleId,String allModule){
        String[] modules = allModule.split(",");
        for (String module : modules) {
            RoleModule roleModule = new RoleModule();
            roleModule.setRoleId(roleId);
            roleModule.setModuleId(Integer.parseInt(module));
            roleInfoMapper.addOneRoleModule(roleModule);
        }

    }


}
